package cn.szzsi.business.controller.admin;

import cn.szzsi.AConfig;
import cn.szzsi.business.service.MemberService;
import cn.szzsi.common.util.*;
import cn.szzsi.model.*;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.redis.Redis;
import javafx.scene.control.Skin;
import org.apache.commons.lang3.StringUtils;

import javax.management.AttributeList;
import javax.servlet.http.Cookie;
import java.util.*;

/**
 * 前台类 - 会员中心
 * 
 */
public class MemberCenterController extends BaseAdminController<Member> {

	public void old(){//商户中心
		Member member = getLoginMember();
		setAttr("loginMember", member);
		setAttr("sc", SiteConfig.dao.findByShopId(getUid()));
		setAttr("uid",getUid());
		//检测是否是商户
		String shopId=member.getStr("shopId");
		if("0".equals(shopId)){
			setAttr("rankStr", MemberService.service.filterRoleResource(shopId,member));
			render(CommonUtil.TEMPLATE_PATH+"/business/main/member_business_center_index.html");
		}else{
			renderNull();
		}
	}

	/**
	 * 版本号2
	 */
	public void indexv2(){
		String shopId = getAttr("shopId");
		Member member = getLoginMember();
		setAttr("uid",shopId);
		setAttr("loginMember", member);
		setAttr("plateformConfig", PlateformConfig.dao.findById(1));
		SiteConfig sc=SiteConfig.getByShopId(shopId);
		setAttr("sc", sc);

		//账号到期时间显示问题
		CompanyInfo ci=CompanyInfo.dao.getByShopId(shopId);
		Long registerTime=ci.getCreateDate().getTime()/1000;
		String status=ci.getStatus();//审核状态:0-适用,1-审核中,2-审核通过,3-审核不通过
		Long countDown=(registerTime+(5*24*60*60))-(new Date().getTime()/1000);
		//Long countDown=(registerTime+(1800))-(new Date().getTime()/1000);

		setAttr("status",status);
		setAttr("tipsInfo",ci.getTipsInfo());
		setAttr("brandLogo",ci.getBrandLogo());

		//检测是否是商户 0说明是商户
		if("0".equals(member.getStr("shopId"))){
			MemberService.service.filterV2RoleResource(getRequest(),shopId,member);
			if("0".equals(status) && countDown<=0){//试用期,并且倒计时小于或者等于0,跳转到认证界面
				redirect("/shop/companyinfo/certificationForm");
			}else if("1".equals(status) && countDown<=0){//审核中,并且倒计时小于或者等于0,跳转到审核中界面
				redirect("/shop/companyinfo/incertificationForm");
			}else if("3".equals(status) && countDown<=0){//审核不通过,并且倒计时小于或者等于0,跳转到认证界面
				redirect("/shop/companyinfo/failverified");
			}else{
				if(countDown<0){//试用期倒计时,如果小于0,则置为0
					countDown=0L;
				}
				render(CommonUtil.TEMPLATE_PATH+"/business/main/indexv2.html");
			}
			setAttr("countDown",countDown);

		}else{
			renderNull();
		}
	}

	/**
	 * 版本号3
	 */
	public void indexv3(){
		String shopId = getAttr("shopId");
		Member member = getLoginMember();
		setAttr("uid",shopId);
		setAttr("loginMember", member);
		setAttr("plateformConfig", PlateformConfig.dao.findById(1));
		SiteConfig sc=SiteConfig.getByShopId(shopId);
		setAttr("sc", sc);
		//账号到期时间显示问题
		CompanyInfo ci=CompanyInfo.dao.getByShopId(shopId);
		setAttr("brandLogo",ci.getBrandLogo());

		//检测是否是商户 0说明是商户
		render(CommonUtil.TEMPLATE_PATH+"/business/main/indexv3.html");
	}

	/**
	 * v3json
	 */
	public void indexv3Json(){
		String shopId = getAttr("shopId");
		Member member = getLoginMember();
		List<Object> firstList= CacheKit.get(EHcacheUtil.RESOURCE_MENU,member.getRoleId());
		if(firstList==null){
			firstList=new ArrayList();
			//将一级菜单放入json,并存入缓存
			//查询所有资源
			List<Resource> firstMenu = Resource.dao.getByshopIdAndLevelAndRoleId(shopId,1,member);//一级菜单
			for(Resource firstResource:firstMenu){

				//根据一级菜单查询二级菜单
				List<Resource> secondMenu=Resource.dao.getByPidAndRoleId(firstResource.getId(),member);
				List<Object> secondList=new ArrayList();
				for(Resource secondResource:secondMenu){

					//根据二级菜单查询三级菜单
					List<Resource>threeMenu=Resource.dao.getByPidAndRoleId(secondResource.getId(),member);
					List<Object>threeList=new ArrayList();
					for(Resource threeResource:threeMenu){
						Map threeMap=new HashMap();
						threeMap.put("thirdDary",threeResource.getName());
						threeMap.put("page",threeResource.getUrl());
						threeList.add(threeMap);
					}

					Map seonndMap=new HashMap();//二级菜单map
					seonndMap.put("secondDary",secondResource.getName());
					seonndMap.put("icon",secondResource.getIcon());
					//seonndMap.put("color",secondResource.getIconColor());
					seonndMap.put("list",threeList);
					secondList.add(seonndMap);


				}

				Map firstMap=new HashMap();//一级菜单map
				firstMap.put("id",firstResource.getId());
				firstMap.put("firstdary",firstResource.getName());
				firstMap.put("list",secondList);
				firstList.add(firstMap);

			}
			CacheKit.put(EHcacheUtil.RESOURCE_MENU,member.getRoleId(),firstList);
		}
		System.out.println(JsonKit.toJson(firstList));
		renderJson(JsonKit.toJson(firstList));


	}


	public void v4(){
		render("/home/index.html");
	}

	/**
	 * v4json(新版view)
	 */
	//@Clear
	public void indexv4Json(){
		String shopId = getAttr("shopId");
		Member member = getLoginMember();
		List<Object> firstList= CacheKit.get(EHcacheUtil.RESOURCE_MENU,member.getRoleId());
		if(firstList==null){
			firstList=new ArrayList();
			//将一级菜单放入json,并存入缓存
			//查询所有资源
			List<Resource> firstMenu = Resource.dao.getByshopIdAndLevelAndRoleId(shopId,1,member);//一级菜单
			//for(Resource firstResource:firstMenu){
			for(int i=0;i<firstMenu.size();i++){
				Resource firstResource=firstMenu.get(i);
				//根据一级菜单查询二级菜单
				List<Resource> secondMenu=Resource.dao.getByPidAndRoleId(firstResource.getId(),member);
				List<Object> secondList=new ArrayList();
				//for(Resource secondResource:secondMenu){
				for(int j=0;j<secondMenu.size();j++){
					Resource secondResource=secondMenu.get(j);
					//根据二级菜单查询三级菜单
					List<Resource>threeMenu=Resource.dao.getByPidAndRoleId(secondResource.getId(),member);
					List<Object>threeList=new ArrayList();
					//for(Resource threeResource:threeMenu){
					for(int k=0;k<threeMenu.size();k++){
						Resource threeResource=threeMenu.get(k);
						Map threeMap=new HashMap();
						if(0==i&&0==j&&0==k){
							threeMap.put("active",1);
						}else{
							threeMap.put("active",0);
						}
						threeMap.put("routers",threeResource.getId());
						threeMap.put("thirdDary",threeResource.getName());
						threeMap.put("page",threeResource.getUrl());

						threeList.add(threeMap);
					}

					Map seonndMap=new HashMap();//二级菜单map
					seonndMap.put("active",0);
					seonndMap.put("secondDary",secondResource.getName());
					seonndMap.put("icon",secondResource.getIcon());
					seonndMap.put("color",secondResource.getIconColor());
					seonndMap.put("list",threeList);
					secondList.add(seonndMap);


				}

				Map firstMap=new HashMap();//一级菜单map
				firstMap.put("id",firstResource.getId());
				firstMap.put("firstdary",firstResource.getName());
				firstMap.put("list",secondList);
				firstList.add(firstMap);

			}
			CacheKit.put(EHcacheUtil.RESOURCE_MENU,member.getRoleId(),firstList);
		}
		//System.out.println(JsonKit.toJson(firstList));
		renderJson(JsonKit.toJson(firstList));


	}






	public void profile(){//会员中心
		Member member = getLoginMember();
		setAttr("loginMember", member);
		setAttr("systemConfig", SystemConfigUtil.getSystemConfig());
		setAttr("base", getRequest().getContextPath());
		//检测是否是商户
		setAttr("uid",getUid());
		render(CommonUtil.TEMPLATE_PATH+"/business/main/member_business_profile.html");

	}


	// 获取当前登录会员，若未登录则返回null
	/*public Member getLoginMember() {
		String id = getSessionAttr(Member.LOGIN_MEMBER_ID_SESSION_NAME);
		if (StringUtils.isEmpty(id)) {
			return null;
		}
		Member loginMember = Member.dao.findById(id);
		return loginMember;
	}*/



	//商户会员注销(old)
	/*public void logout() {
		getRequest().getSession().removeAttribute(Member.LOGIN_MEMBER_ID_SESSION_NAME);
		Cookie cookie = new Cookie(Member.LOGIN_MEMBER_USERNAME_COOKIE_NAME, null);
		Cookie loginTypeCookie=new Cookie(Member.LOGIN_TYPE_COOKIE_NAME,null);
		cookie.setPath(getRequest().getContextPath() + "/");
		loginTypeCookie.setPath(getRequest().getContextPath() + "/");
		cookie.setMaxAge(0);
		loginTypeCookie.setMaxAge(0);
		getResponse().addCookie(cookie);
		getResponse().addCookie(loginTypeCookie);
		redirect("/admin/login");
	}*/

	//商户会员注销(new)
	@Clear
	public void logout() {
		//移除会员登录名Cookie和登录类型Cookie
		Cookie loginMemberUsernameCookie = new Cookie(Member.LOGIN_MEMBER_USERNAME_COOKIE_NAME, null);
		Cookie loginTypeCookie=new Cookie(Member.LOGIN_TYPE_COOKIE_NAME,null);
		loginMemberUsernameCookie.setPath(getRequest().getContextPath() + "/");
		loginTypeCookie.setPath(getRequest().getContextPath() + "/");
		loginMemberUsernameCookie.setMaxAge(0);
		loginTypeCookie.setMaxAge(0);
		getResponse().addCookie(loginMemberUsernameCookie);
		getResponse().addCookie(loginTypeCookie);

		//移除会员登陆_auth(可以凭借_auth换取用户信息)
		Cookie authCookie = new Cookie("_auth",null);//登录类型
		authCookie.setPath(getRequest().getContextPath()+"/");
		authCookie.setMaxAge(0);
		setCookie(authCookie);

		String username=getCookie("username");
		if(StrKit.notBlank(username)){//如果商户别名不为空,将其清除!
			Cookie usernameCookie = new Cookie("username",null);//登录类型
			usernameCookie.setPath(getRequest().getContextPath()+"/");
			usernameCookie.setMaxAge(0);
			setCookie(usernameCookie);
		}else{
			username="";
		}
		redirect("/login/"+username+"");
	}

	public static void main(String[] args) {
		System.out.println(Ret.ok());
	}
}
